<html>
<head><meta charset="utf-8"><title>require_consts evaluation on mir vs codegen · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html">require_consts evaluation on mir vs codegen</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239666687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239666687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239666687">(May 20 2021 at 22:08)</a>:</h4>
<p><span aria-label="wave" class="emoji emoji-1f44b" role="img" title="wave">:wave:</span>, I wanted to better understand why do we evaluate required_consts in 2 different ways in different parts of the code</p>



<a name="239666720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239666720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239666720">(May 20 2021 at 22:08)</a>:</h4>
<p>we do <a href="https://github.com/rust-lang/rust/blob/9a3214e9be41a5d50ae6ba9bb8422dcc2cb10473/compiler/rustc_codegen_ssa/src/mir/mod.rs#L192">https://github.com/rust-lang/rust/blob/9a3214e9be41a5d50ae6ba9bb8422dcc2cb10473/compiler/rustc_codegen_ssa/src/mir/mod.rs#L192</a> and <a href="https://github.com/rust-lang/rust/blob/9a3214e9be41a5d50ae6ba9bb8422dcc2cb10473/compiler/rustc_mir/src/interpret/eval_context.rs#L692">https://github.com/rust-lang/rust/blob/9a3214e9be41a5d50ae6ba9bb8422dcc2cb10473/compiler/rustc_mir/src/interpret/eval_context.rs#L692</a></p>



<a name="239666778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239666778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239666778">(May 20 2021 at 22:09)</a>:</h4>
<p>and I'm probably the one that implemented that but I don't remember anymore, lol</p>



<a name="239666864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239666864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239666864">(May 20 2021 at 22:10)</a>:</h4>
<p>I meant, besides what the comments say that in one case it could error and in the other it can't, I'm trying to understand how much those things diverge and if they should really diverge</p>



<a name="239672853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239672853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239672853">(May 20 2021 at 23:07)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="189540" href="/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/evaluate.20require_consts">#t-compiler/wg-mir-opt &gt; evaluate require_consts</a> by <span class="user-mention silent" data-user-id="116266">Santiago Pastorino</span></p>



<a name="239673349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239673349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239673349">(May 20 2021 at 23:11)</a>:</h4>
<p>well the second one does monomorphization, but I guess I'd expect the first one doing something similar to what's done to the first one in regard <code>Unevaluated</code> consts?</p>



<a name="239724265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239724265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239724265">(May 21 2021 at 09:51)</a>:</h4>
<p>All constants in <code>required_consts</code> are <code>Unevaluated</code> I think. It lists all constants that are required to succeed evaluation after monomorphization. The code in cg_ssa checks this during codegen. The code in rustc_mir checks this during const eval.</p>



<a name="239771119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239771119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239771119">(May 21 2021 at 15:43)</a>:</h4>
<p>yeah, all are <code>Unevaluated</code>, I was wondering mainly about why those evaluations differ that much but <span class="user-mention" data-user-id="124288">@oli</span> told me that they eval for different representation which make sense :)</p>



<a name="239867617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867617">(May 22 2021 at 11:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116266">Santiago Pastorino</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen/near/239666864">said</a>:</p>
<blockquote>
<p>I meant, besides what the comments say that in one case it could error and in the other it can't, I'm trying to understand how much those things diverge and if they should really diverge</p>
</blockquote>
<p>it can error in both cases though...</p>



<a name="239867638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867638">(May 22 2021 at 11:51)</a>:</h4>
<p>but yeah the representation is the main difference... though if you follow the method chains a bit they should bottom out at the same thing</p>



<a name="239867681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867681">(May 22 2021 at 11:52)</a>:</h4>
<p>miri and codegen both have helper functions to evaluate consts, called <code>mir_const_to_op</code> and <code>eval_mir_constant</code> respectively</p>



<a name="239867803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867803">(May 22 2021 at 11:54)</a>:</h4>
<p>hm, actually they don't -- Miri/CTFE calls <code>eval_to_allocation</code> but codegen calls <code>eval_to_const_value_raw</code></p>



<a name="239867811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867811">(May 22 2021 at 11:55)</a>:</h4>
<p>Miri/CTFE could go through <code>eval_to_const_value_raw</code> I guess but the other path was easier^^</p>



<a name="239867829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/239867829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#239867829">(May 22 2021 at 11:55)</a>:</h4>
<p>for codegen, <code>eval_to_const_value_raw</code> is what you want so that you dont end up creating allocations for literals like <code>42u32</code></p>



<a name="240660222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240660222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240660222">(May 28 2021 at 21:02)</a>:</h4>
<p>I see :)</p>



<a name="240660240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240660240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240660240">(May 28 2021 at 21:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen/near/239867811">said</a>:</p>
<blockquote>
<p>Miri/CTFE could go through <code>eval_to_const_value_raw</code> I guess but the other path was easier^^</p>
</blockquote>
<p>are you suggesting to try to make this change?</p>



<a name="240695442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240695442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240695442">(May 29 2021 at 10:31)</a>:</h4>
<p>I'm honestly not sure of all the code flows here currently (it's a bit of a mess), but I suggest we wait until after the valtree change before cleaning that up</p>



<a name="240699864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240699864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240699864">(May 29 2021 at 12:30)</a>:</h4>
<p>what/where is the valtree change?</p>



<a name="240700599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240700599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240700599">(May 29 2021 at 12:48)</a>:</h4>
<p>some older notes on valtrees: <a href="https://hackmd.io/Qvrj_eOFTkCHZrhJ7f1ItA">https://hackmd.io/Qvrj_eOFTkCHZrhJ7f1ItA</a></p>



<a name="240700604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/require_consts%20evaluation%20on%20mir%20vs%20codegen/near/240700604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/require_consts.20evaluation.20on.20mir.20vs.20codegen.html#240700604">(May 29 2021 at 12:48)</a>:</h4>
<p>oli's latest PR: <a href="https://github.com/rust-lang/rust/pull/83234">https://github.com/rust-lang/rust/pull/83234</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>